package com.cms.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cms.entity.ModelField;
import com.cms.mapper.ModelFieldMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 内容字段表 服务实现类
 * </p>
 *
 * @author songfayuan
 * @since 2021-11-12
 */
@Service
public class ModelFieldService extends ServiceImpl<ModelFieldMapper, ModelField> {

    /**
     * 判断名称是否存在
     *
     * @param name
     *            名称
     * @return 名称是否存在
     */
    public boolean nameExists(Integer modelId,String name) {
        if (StringUtils.isBlank(name)) {
            return false;
        }
        QueryWrapper<ModelField> queryWrapper = new QueryWrapper();
        queryWrapper.eq("modelId",modelId);
        queryWrapper.eq("name",name);
        Long count = baseMapper.selectCount(queryWrapper);
        return count > 0;
    }

    /**
     * 查找模型字段
     *
     * @param modelId
     * 				模型ID
     * @return 模型字段
     */
    public List<ModelField> findList(Integer modelId){
        QueryWrapper<ModelField> queryWrapper = new QueryWrapper();
        if(modelId != null){
            queryWrapper.eq("modelId",modelId);
        }
        queryWrapper.orderByDesc("sort","createDate");
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 根据模型ID删除模型属性
     *
     * @param modelId
     */
    public void deleteByModelId(Integer modelId){
        Map<String,Object> columnMap = new HashMap<>();
        columnMap.put("modelId",modelId);
        baseMapper.deleteByMap(columnMap);
    }
}
